#include "MovableRect.h"
#include "MaterialManager.h"
#include "GpuProgramManager.h"
#include "VideoManager.h"

namespace DreamEngine
{
struct strData
{
	float x, y, z;
};

CTestMovable::CTestMovable(CRenderSystem* pRenderSystem, const CString& name):
	CMovableObject(pRenderSystem, name)
{
	
}


CTestMovable::~CTestMovable(void)
{
}

void CTestMovable::OnCreate()
{
	strData data[3];
	data[0].x=0.0f; data[0].y=0.0f; data[0].z=1.0f;
	data[1].x=0.0f; data[1].y=1.0f; data[1].z=1.0f;
	data[2].x=1.0f; data[2].y=1.0f; data[2].z=1.0f;
	 
	CRenderData* pRenderData = new CRenderData;
	m_pMaterial = pRenderData->m_pMaterial = CMaterialManager::InstancePtr()->Create("default");

	CVertexBuffer* pVertexBuffer = CVideoManager::InstancePtr()
		->CreateVertexBuffer(data, 3, sizeof(strData));
	pRenderData->AddVertexBuffer(0, pVertexBuffer);

	CGpuProgram* pVertexProgram = CGpuProgramManager::InstancePtr()->CreateGpuProgram(SP_VS_2_0);
	if (IsEngineErrorOk(pVertexProgram->Initialize
	("DefaultHLSL_Vertex.hlsl", "VS_MAIN")))
	{
		pRenderData->m_pMaterial->SetVertexProgram(pVertexProgram);
	}

	CGpuProgram* pFragmentProgram = CGpuProgramManager::InstancePtr()->CreateGpuProgram(SP_PS_2_0);
	if (IsEngineErrorOk(pFragmentProgram->Initialize
	("DefaultHLSL_Pixel.hlsl", "PS_MAIN")))
	{
		pRenderData->m_pMaterial->SetFragmentProgram(pFragmentProgram);
	}

	CVertexElement elem[2] = {	CVertexElement(0, 0, DREAMDECLTYPE_FLOAT3,DREAMDECLMETHOD_DEFAULT,DREAMDECLUSAGE_POSITION,0 ),
							    CVertexElement(0, 0, DREAMDECLTYPE_FLOAT2,DREAMDECLMETHOD_DEFAULT,DREAMDECLUSAGE_TEXCOORD,0 )};
	pRenderData->AddElement(elem, 2);

	m_renderData.push_back(pRenderData);

	m_trangleCount = CaculateTrangleCount();
}

void CTestMovable::OnLostDevice()
{

}

void CTestMovable::OnResetDevice()
{

}

void CTestMovable::OnDestroy()
{
	RenderDataLst::iterator iter;
	for (iter=m_renderData.begin(); iter!=m_renderData.end(); ++iter)
	{
		SAFE_DELETE(*iter);
	}
}

}
